package com.chen.nongansampling.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.SparseArray;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProviders;

import com.chen.nongansampling.R;
import com.chen.nongansampling.adapter.SampledRecycleViewAdapter;
import com.chen.nongansampling.fragment.SampledFragment;
import com.chen.nongansampling.fragment.ui.sample.SampleFragment;
import com.chen.nongansampling.utils.CommandPhotoUtil;
import com.chen.nongansampling.utils.PhotoSystemOrShoot;
import com.chen.nongansampling.viewModel.SamplingViewModel;

public class SampleActivity extends AppCompatActivity {

    SampledRecycleViewAdapter adapter = new SampledRecycleViewAdapter();
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);


        setContentView(R.layout.sample_activity);
        SamplingViewModel model = ViewModelProviders.of(this).get(SamplingViewModel.class);

        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏标题
        toolbar.setNavigationIcon(R.mipmap.back_white);       //加载图标
        toolbar.setTitle("");
        toolbar.setTitleTextColor(Color.WHITE);


        /* wxs注销掉
        if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, SampledFragment.newInstance())
//                .addToBackStack("SampleFragment")
                .commitNow();

         */
        FragmentManager mFragmentManager;
        mFragmentManager = getSupportFragmentManager();

        SampledFragment sdf;
        sdf =(SampledFragment)mFragmentManager.findFragmentByTag("wxsSampledFragment");
        if(sdf!=null){
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.remove(sdf);
            fragmentTransaction.commit();
        }
        SampleFragment theFragMent;
        theFragMent =(SampleFragment)mFragmentManager.findFragmentByTag("wxsSampleFragment");
        if(theFragMent!=null){
            FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
            fragmentTransaction.remove(theFragMent);
            fragmentTransaction.commit();
        }

        mFragmentManager.beginTransaction()
                .add(R.id.container, SampledFragment.newInstance(),"wxsSampledFragment")
//                .addToBackStack("SampleFragment")
                .commitNow();

    }

//    @Override
//    protected void onSaveInstanceState(Bundle outState) {
//        outState.putString("k1", "113");
//        super.onSaveInstanceState(outState);
//        outState.putString("k2", "113-k2");
//    }



    private PhotoSystemOrShoot selectPhoto;
    private CommandPhotoUtil commandPhoto;

//        @Override
//    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//
//        // 获取照片返回
//        if (selectPhoto != null) {
//            String photoPath = selectPhoto.getPhotoResultPath(requestCode, resultCode, data);
//            if (!TextUtils.isEmpty(photoPath)) {
//                commandPhoto.showGridPhoto(photoPath);
//            }
//        }
//    }



    final String STATE_ = "state_";
    SparseArray<Fragment.SavedState> savedStateSparseArray = new SparseArray<>();

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putSparseParcelableArray(STATE_, savedStateSparseArray);
}

    /**
     * 手动存状态
     *
     * @param index
     * @param fragment
     */
    private void saveFragmentState(int index, Fragment fragment) {
        Fragment.SavedState savedState = getSupportFragmentManager().saveFragmentInstanceState(fragment);
        savedStateSparseArray.put(index, savedState);
    }

    /**
     * 手动调用 恢复状态
     *
     * @param index
     * @param fragment
     */
    private void restoreFragmentState(int index, Fragment fragment) {
        Fragment.SavedState savedState = savedStateSparseArray.get(index);
        fragment.setInitialSavedState(savedState);
    }

}
